require 'chord'

chord = Chord.new(5, 'localhost', 9005, 4)
chord.succs_added do |id, node, first|
  puts "#==================================================================#"
  puts "Eu " + id
  puts "Recebi um Novo Successor:"
  puts node[0]
  puts node[1]
  puts "Ele eh o primeiro : " + first.to_s
  puts "#==================================================================#"
end

chord.succs_leaved do |id, node, first|
  puts "#==================================================================#"
  puts "Eu " + id
  puts "Perdi um Successor"
  puts node[0]
  puts node[1]
  puts "Ele eh o primeiro : " + first.to_s
  puts "#==================================================================#"
end

chord.new_predecessor do |id, pred|
  puts "#==================================================================#"
  puts "EU " + id 
  puts "RECEBI UM NOVO PREDECESSOR " + pred
  puts "#==================================================================#"
end

chord.join('localhost', 9000)


puts "aperte x para sair"

while true
  op = gets
  if op == "x\n"
    break
  elsif op == "l\n"
    node = chord.lookup("localhost:9001")
    puts "LOOKUP NODE: " + node
  else
    chord.print_nodes
  end
  puts ""
  puts "aperte x para sair"
end
